<?php

class Commission_accountModel extends BaseModel
{
    var $table = 'commission_account';
    var $prikey = 'id';
    var $_name = 'commission_account';
    var $_relation = array(
        'has_member_commission' => array(
            'model' => 'member',
            'type' => BELONGS_TO,
            'foreign_key' => 'user_id',
            'reverse' => 'user_commission_account',
        ),
    );

    //待审核
    const STATUS_REVIEWED = 1;
    //待付款
    const STATUS_TO_PAID = 2;
    //已付款
    const STATUS_PAID = 3;

    // 设置映射关系
    public static $statusMap = array(
        self::STATUS_REVIEWED => '待审核',
        self::STATUS_TO_PAID => '待付款',
        self::STATUS_PAID => '已付款',
    );


    //封装三方打款方法
    function makeModel(){

        list($msec, $sec) = explode(' ', microtime());
        $msectime = (float)sprintf('%.0f', (floatval($msec) + floatval($sec)) * 1000);

        $arr['appid'] = '1231312313123';
        $arr['version'] = '1.0';
        $arr['nonce'] = $this->getNonceStr();
        $arr['timestamp'] = $msectime;
        $arr['data'] = array(
            "aliUserId"           => "29847DEFDDD74EA8A5C35904D4004DF5",
            "aliUserType"         => "ALIPAY_USER_ID",
            "money"               => "100",
            "outTradeNo"          => "11132020010815033910010100000007",
            "payeeType"           => "alipay",
            "taskRemark"          => "推广佣金",
            "taxCollectionProviderId"=> "51B85456549999D69A13455FE707F",
            "taxCollectionProviderName"=> "嘿多钱代征公司",
        );

        $res = $this->asc_sort($arr);
        //签名步骤三：MD5加密
        $string = md5($res);
        //签名步骤四：所有字符转为大写
        $result = strtoupper($string);
        $arr['sign'] = $result;
        $arr['signType'] = 'RSA2';
        dump($arr);
        $url = 'http://192.168.2.239:8406/gs_capital_web/openapi/gateway/pay ';

        $res = $this->sync_data($url,$arr);
        dump($res);

    }

    /**curl OMS请求**/
    function sync_data($url, $data_string)
    {
        $data_string = json_encode($data_string);
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // https请求 不验证证书和hosts
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        curl_setopt($ch, CURLOPT_HTTPHEADER, array(
                'Content-Type: application/json; charset=utf-8',
                'Content-Length: ' . strlen($data_string))
        );
        ob_start();
        $res = curl_exec($ch);
        if (!$res) {
            return false;
        } else {
            return json_decode($res,true);
        }
        return array('code' => $return_code, 'result' => $return_content);
    }


    function getNonceStr($length = 32)

    {
        $chars = "abcdefghijklmnopqrstuvwxyz0123456789";
        $str ="";
        for ( $i = 0; $i < $length; $i++ )  {
            $str .= substr($chars, mt_rand(0, strlen($chars)-1), 1);
        }
        return $str;
    }

    function asc_sort($params = array())
    {
        if (!empty($params)) {
            $p = ksort($params);
            if ($p) {
                $str = '';
                foreach ($params as $k => $val) {
                    if(is_array($val)){
                        $b = ksort($val);
                        $str .= $k.'={';
                        foreach($val as $key=>$value){
                            $str .= $key . '=' . $value . '&';
                        }
                        $arr = rtrim($str,'&');
                        $str = $arr.'}';
                    }else{
                        $str .= $k . '=' . $val . '&';
                    }
                }
                $strs = rtrim($str, '&');
                return $strs;
            }

        }
        return false;
    }



}